package statistics;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Statistiky {

	String suborSvysledkom;
	String mojSuborSvysledkom;

	double odchylka = 0;
	double maxOdchylka = 1;
	double priemernaOdchylka = 0;

	public Statistiky(String suborSvysledkom, String mojSuborSvysledkom) {

		this.suborSvysledkom = suborSvysledkom;
		this.mojSuborSvysledkom = mojSuborSvysledkom;
	}

	public void zistiStatistiky() {

		BufferedReader subor1 = null;
		BufferedReader subor2 = null;
		StringTokenizer tok1 = null;
		StringTokenizer tok2 = null;
		int riadokSmaxOdchylkou = 0;

		try {
			subor1 = new BufferedReader(new InputStreamReader(
					new DataInputStream(new FileInputStream(suborSvysledkom))));
			subor2 = new BufferedReader(
					new InputStreamReader(new DataInputStream(
							new FileInputStream(mojSuborSvysledkom))));
			String subor1Riadok = null;
			String subor2Riadok = null;
			int cisloRiadku = 0;
			while ((subor1Riadok = subor1.readLine()) != null) {
				subor2Riadok = subor2.readLine();
				cisloRiadku++;
				tok1 = new StringTokenizer(subor1Riadok, " ");
				tok2 = new StringTokenizer(subor2Riadok, " ");
				tok1.nextToken();
				tok1.nextToken();
				int cenaRiesenie = Integer.parseInt(tok1.nextToken());
				tok2.nextToken();
				tok2.nextToken();
				int cenaMojeRiesenie = Integer.parseInt(tok2.nextToken());
				priemernaOdchylka += Math.abs(cenaRiesenie - cenaMojeRiesenie);
				odchylka = ((double) cenaMojeRiesenie / (double) cenaRiesenie);
				if (odchylka < maxOdchylka) {
					maxOdchylka = odchylka;
					riadokSmaxOdchylkou = cisloRiadku;
				}
				
			
			} // koniec while pre vsetky riadky
			System.out.println("Porovnavacie statistiky pre subory "
					+ suborSvysledkom + " a " + mojSuborSvysledkom);
			System.out.println("Max odchylka je "
					+ ((int) ((1 - maxOdchylka) * 100)) / 100.0
					+ " a nastala u riadku " + riadokSmaxOdchylkou);
			System.out.println("Priemerna odchylka je "
					+ (priemernaOdchylka / 50));

		} catch (FileNotFoundException ex) {
			Logger.getLogger(Statistiky.class.getName()).log(Level.SEVERE,
					null, ex);
		} catch (IOException ex) {
			Logger.getLogger(Statistiky.class.getName()).log(Level.SEVERE,
					null, ex);
		} finally {
			try {
				subor1.close();
				subor2.close();
			} catch (IOException ex) {
				Logger.getLogger(Statistiky.class.getName()).log(Level.SEVERE,
						null, ex);
			}
		}

	}
}
